﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Collections.Generic;

public partial class mainMenu : AbstractPage {
	protected List<Schedule> schedules;

	protected void Page_Load(object sender, EventArgs e) {
		User user = new User();

		try {
			user = (User) this.Session[AbstractPage.SESSION_USER];

            if (user == null) {
                this.messages.Add(new Message(Message.Type.WARNING, "Please log in to continue"));
                this.Server.Transfer("index.aspx", true);
            }

			lblName.Text = user.LastName + ", " + user.FirstName + " " + user.MiddleName;
			lblEmail.Text = user.Email;
			if ((user.Destinations != null) && (user.Destinations.Count > 0)) {
				Destination destination = user.Destinations[0];
				this.lblZip.Text = destination.Zip.ToString();
			}

			//Sort the user's schedules
			user.Schedules.Sort(new ScheduleComparer());
			this.schedules = user.Schedules;

			//Query to see if there are any compatible schedules in the system
			IScheduleService scheduleService = new ScheduleServiceImpl();
			this.ctrlSearchResults.InputSchedules = scheduleService.findCompatibleSchedules(user);
		} catch (Exception ex) {
			this.messages.Add(new Message(Message.Type.ERROR, ex.Message));
			//this.messages.Add(new Message(Message.Type.ERROR, ex.StackTrace));
		}
	}

	protected void btnLogOut_Click(object sender, EventArgs e) {
        this.Session.Remove(AbstractPage.SESSION_USER);
        this.messages.Add(new Message(Message.Type.INFO, "You have logged out"));
		this.Session.Remove(AbstractPage.SESSION_USER);
        this.Server.Transfer("index.aspx", true);
	}

	protected void btnSearch_Click(object sender, EventArgs e) {
		this.Response.Redirect("search.aspx");
	}

	protected void btnUpdateAccount_Click(object sender, EventArgs e) {
		this.Response.Redirect("profile.aspx");
	}

	protected void linkEditSchedule_Click(object sender, EventArgs e) {
		this.btnUpdateAccount_Click(sender, e);
	}

	protected void linkEditProfile_Click(object sender, EventArgs e) {
		this.btnUpdateAccount_Click(sender, e);
	}
}
